/*******************************************************************************
 * Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package dataflowScheme.diagram.customs;

import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.draw2d.ui.figures.FigureUtilities;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;

/**
 * intended to layout ports inside portset
 * 
 * @author <a href="mailto:misiak7@gmail.com">Michal Antolik</a>
 *
 */
public class HorizontalStackLayout extends FreeformLayout {

	public HorizontalStackLayout() {
		super();
	}
	
	@SuppressWarnings("unchecked")
	public void layout(IFigure parent) {
		List childrenList = parent.getChildren();
        Iterator children = childrenList.iterator();
        IFigure f;

        Rectangle parentBound = parent.getClientArea();
        Dimension textDimension = null;
        WrappingLabel wl = null;
        Shape shape = null;
        
        while(children.hasNext()){
        	f = (IFigure)children.next();
        	if(f instanceof WrappingLabel){
        		wl = (WrappingLabel)f;
        	} else if (f instanceof RectangleFigure){
        		shape = (RectangleFigure)f;
        	} else if (f instanceof Ellipse){
        		shape = (Ellipse)f;
        	} 
        }
        
        boolean in = true;
        if(parent instanceof ConnectionTypeFigure){
        	String t = ((ConnectionTypeFigure) parent).getConnectionType();
        	if(t.equals("Out")){
        		in = false;
        	}
        }
        textDimension = FigureUtilities.getTextExtents(wl.getText(), wl.getFont());
        int textY = 0;
        int padding = 6;
        int offset = parentBound.x + padding;
        
        Rectangle bounds = wl.getBounds().getCopy();
        textY = bounds.y;
        bounds.x = offset + (in?10 + padding:0);//0;
        bounds.width = textDimension.width;
        bounds.height = textDimension.height;
        wl.setBounds(bounds);
        
        bounds = shape.getBounds().getCopy();
        bounds.y = textY+5;
        bounds.x = offset + (in?0:textDimension.width + padding);//textDimension.width;
        bounds.width = 10;
        bounds.height = 10;//height > 18?height:18;//textDimension.height;
        shape.setBounds(bounds);
	}
	
	/**
	 * @see org.eclipse.draw2d.AbstractLayout#calculatePreferredSize(org.eclipse.draw2d.IFigure, int, int)
	 */
	@SuppressWarnings("unchecked")
	protected Dimension calculatePreferredSize(
		IFigure container,
		int wHint,
		int hHint) {

        int padding = 6;

		int iconWidth = 10;
		
		List childrenList = container.getChildren();
        Iterator children = childrenList.iterator();
        IFigure f;

        WrappingLabel wl = null;
        
        while(children.hasNext()){
        	f = (IFigure)children.next();
        	if(f instanceof WrappingLabel){
        		wl = (WrappingLabel)f;
        	}
        }
        if(wl != null){
        	Dimension textDimension = FigureUtilities.getTextExtents(wl.getText(), wl.getFont());
        	textDimension.width += iconWidth + 3*padding;
        	return textDimension;
        }else{
        	return new Dimension(140, 20);
        }
        
		
	}

	/**
	 * @see org.eclipse.draw2d.AbstractHintLayout#calculateMinimumSize(org.eclipse.draw2d.IFigure, int, int)
	 */
	public Dimension calculateMinimumSize(
		IFigure container,
		int wHint,
		int hHint) {
		return getPreferredSize(container, wHint, hHint);
	}
	
}
